package com.ego.dubbo.service.impl;

import com.ego.commons.exception.DaoException;
import com.ego.dubbo.service.TbItemDubboService;
import com.ego.mapper.TbItemDescMapper;
import com.ego.mapper.TbItemMapper;
import com.ego.mapper.TbItemParamItemMapper;
import com.ego.pojo.TbItem;
import com.ego.pojo.TbItemDesc;
import com.ego.pojo.TbItemParamItem;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


//这个注解表示吧当前类实现的接口发布到zookeeper中
@Service
public class TbItemDubboServiceImpl implements TbItemDubboService {

    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbItemDescMapper tbItemDescMapper;
    @Autowired
    private TbItemParamItemMapper tbItemParamItemMapper;
    @Override
    public List<TbItem> selectByPage(int pagesize, int pagenum) {
        //分页插件一般都写在第一行
        PageHelper.startPage(pagenum,pagesize);

        //Example 相当于sql中 where   没有参数的时候 设置成null即可
        // select * from tb_item limit ?,?
        List<TbItem> list = tbItemMapper.selectByExample(null);

        //千万别忘记 构造方法参数
        PageInfo<TbItem> pi=new PageInfo<>(list);
        return pi.getList();
    }

    @Override
    public long selectCount() {
        return tbItemMapper.countByExample(null);
    }

    @Override
    //监听到异常 则进行事务回滚 声明试事务
    @Transactional
    public int updateStatusByIds(long[] ids, int status) throws DaoException {
        Date date = new Date();
        int index=0;
        for (long id : ids) {
            TbItem tbItem = new TbItem();
            tbItem.setId(id);
            tbItem.setStatus((byte) status);
            tbItem.setUpdated(date);

            //每次 根据主键id修改
            index+=tbItemMapper.updateByPrimaryKeySelective(tbItem);
        }
        //修改的条数 和数组长度一样 说明全部修改成功了
        if (index==ids.length){
            return 1;
        }
        //抛出异常 让数据回滚
        throw new DaoException("批量修改失败");
    }

    @Override
    @Transactional   //两表添加  声明事务
    public int insert(TbItem tbItem, TbItemDesc tbItemDesc, TbItemParamItem tbItemParamItem) throws  DaoException {

        int index = tbItemMapper.insert(tbItem);
        //如果第一个表添加成功 则继续添加第二个表中的数据
        if (index==1){
            int index2 = tbItemDescMapper.insert(tbItemDesc);
            if (index2==1){
                int insert = tbItemParamItemMapper.insert(tbItemParamItem);
                if (insert==1){
                    return 1;
                }
            }
        }
        throw new DaoException("新增失败!");
    }

    @Override
    @Transactional
    public int update(TbItem tbItem, TbItemDesc tbItemDesc, TbItemParamItem tbItemParamItem) throws DaoException {
        //因为创建时间不能修改 所以选用selective 根据主键选择 动态sql
        int i = tbItemMapper.updateByPrimaryKeySelective(tbItem);
        if (i==1){
            int i1 = tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
            if (i1==1){
                int i2 = tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);
                if (i2==1){
                    return 1;
                }
            }
        }
        throw new DaoException("修改失败!");
    }

    @Override
    public TbItem selectById(Long id) {
        return tbItemMapper.selectByPrimaryKey(id);
    }
}
